/*************************************************************************
Purpose: Loads plot output from GAIA and modifies it in some manner. 
**************************************************************************
Copyright 2010 Bryan Killett

This file is part of GAIA.

GAIA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GAIA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GAIA. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
//Header file "definitions.hpp" declares many functions, constants, datatypes.
//In order to use the functions declared in definitions.hpp, must link
//with object code that includes functions.cpp.
#include "definitions.hpp"

int main(int argc, char *argv[]){  

  //Variable declarations- every variable type that ends in "_s"
  //is defined in GAIAFOLDER/c/definitions.hpp.
  long long i,j,k;
  string input_file(gaiafolder()); //Filename of options file created in IDL.
  input_s input;
  ancillary_s ancillary;
  grid_s region;
  grid_s grid, grid2;
  model_s model;
  results_s results1,results2,final_results;
  plot_options_s plot_options,plot_options2,temp_options;
  insitu_s insitu;//For tide gauge data, etc.
      
  string base;//This string forms the base part of the output filename.

  string input_folder1,input_folder2;
  
  //////////////////////////////////////////////////////////////////////////
  //End of variable declarations, start of initialization/loading section.
  //////////////////////////////////////////////////////////////////////////
  
  //Each program should manually set this value.
  //See definitions.hpp for more details.
  results1.options.output_choice = 2;
  results2.options.output_choice = results1.options.output_choice;
  final_results.options.output_choice = results1.options.output_choice;

  //Controls how old output files will be modified to create new output files.
  //Old output files are found in input_folder1,2.
  //Both folders need to have the same number of files and grid points and
  //locations of grid points.
  //Case 1 doesn't require its folder to have output_filenames.txt, but
  //other cases do.
  //New output files will be saved to folder specified in argv options.
  //  1   - Multiply all y values by parameter1.
  // 21   - Subtract the files in the 2nd listed output folder from the 1st.
  // 22   - Add the files in the 1st and 2nd listed output folders.
  //100   - Convert orig accels + residuals to RELATIVE reduction plots.
  int modification_choice = 100;
  //load_output wants the folder names relative to GAIAFOLDER.
  input_folder1 = "output/temp/";
  //input_folder1 = "output/maps/20100907-152443 - Accels due to Hudson N 130km 500cm diff inf and deg90, mascond using 50km with old areas and weightings/";
  input_folder2 = "output/maps/20100907-152639 - Accels due to Hudson N 130km 500cm diff inf and deg90, mascond using 50km with new areas and weightings/";
  
  //This parameter is used in several modification cases.
  double parameter1=1e9;

  //Use current time and command line args to create output folder.
  create_output_folder(argc,argv,plot_options);
  
  //Sometimes I need to store the outputfolder in another variable temporarily
  string output_folder(plot_options.outputfolder.c_str());

  cout<<"Loading output1 from folder1 "<<gaiafolder()<<input_folder1<<endl;
  if(modification_choice != 1) cout<<"Loading output2 from folder2 "<<gaiafolder()<<input_folder2<<endl;
  cout<<"Saving modified output to "<<plot_options.outputfolder<<endl;

cout<<"!!!NEED TO CP OPTIONS FILE AND MISC_results AND OTHER FILES TO NEW DIR!"<<endl;
  
  //I preset the input folder to 1 because that's what most cases do anyway.
  plot_options.inputfolder = input_folder1;

  switch(modification_choice){
    default:cout<<"WARNING!!! Didn't recognize modification_choice"<<modification_choice<<endl;
    /////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////
    case 1:{
      cout<<"Loading the folder."<<endl;
      load_output(final_results,plot_options,grid);
      //Loop through all plots.
      for(i=0;i<(long long)final_results.xy.y_values.size();i++){
        //Loop through all lines in this plot.
        for(j=0;j<(long long)final_results.xy.y_values.at(i).size();j++){
          //Loop through points in this line.
          for(k=0;k<(long long)final_results.xy.y_values.at(i).at(j).size();k++){
            final_results.xy.y_values.at(i).at(j).at(k) *= parameter1;
          }
        }
      }
      break;
    }
    /////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////
    case 21: //Subtract   two output folders.
    case 22:{//Add        two output folders.
      //First load both folders into their respective results structures.
      cout<<"Loading the first folder."<<endl;
      load_output(results1,plot_options,grid);
      plot_options2.inputfolder = input_folder2;
      cout<<"Loading the second folder."<<endl;
      load_output(results2,plot_options2,grid);

      //Quick check- do both have same number of plots and lines in each plot, and pts per line?
      if(results1.xy.y_values.size() != results2.xy.y_values.size()) cout<<"First folder has "<<results1.xy.y_values.size()<<" plots while second folder has "<<results2.xy.y_values.size()<<" plots."<<endl;
      for(i=0;i<(long long)results1.xy.y_values.size();i++){
        if(results1.xy.y_values.at(i).size() != results2.xy.y_values.at(i).size()) cout<<"First folder has "<<results1.xy.y_values.at(i).size()<<" lines in plot #"<<i+1<<" while second folder has "<<results2.xy.y_values.at(i).size()<<" lines in that plot."<<endl;
        for(j=0;j<(long long)results1.xy.y_values.at(i).size();j++){
          if(results1.xy.y_values.at(i).at(j).size() != results2.xy.y_values.at(i).at(j).size()) cout<<"First folder has "<<results1.xy.y_values.at(i).at(j).size()<<" points in line #"<<j+1<<" in plot #"<<i+1<<" while second folder has "<<results2.xy.y_values.at(i).at(j).size()<<" points in that line."<<endl;
        }
      }

      if(modification_choice == 21){
        cout<<"Calculating folder1 - folder 2"<<endl;
      }
      else if(modification_choice == 22){
        cout<<"Calculating folder1 + folder 2"<<endl;
      }

      //Copy everything from 1st folder.
      final_results = results1;
      
      //Loop through all plots.
      for(i=0;i<(long long)final_results.xy.y_values.size();i++){
        //Loop through all lines in this plot.
        for(j=0;j<(long long)final_results.xy.y_values.at(i).size();j++){
          //Loop through points in this line.
          for(k=0;k<(long long)final_results.xy.y_values.at(i).at(j).size();k++){
            if(modification_choice == 21){
              final_results.xy.y_values.at(i).at(j).at(k) = results1.xy.y_values.at(i).at(j).at(k) - results2.xy.y_values.at(i).at(j).at(k);
            }
            else if(modification_choice == 22){
              final_results.xy.y_values.at(i).at(j).at(k) = results1.xy.y_values.at(i).at(j).at(k) + results2.xy.y_values.at(i).at(j).at(k);
            }
            else cout<<"!!!!WARNING!!! ERROR DETECTED!!!"<<endl;
          }
        }
      }

      //Now modify all titles so they won't be over-written by sin/cos conversion.
      for(i=0;i<(long)final_results.xy.titles.size();i++){
        if(modification_choice == 21){
          final_results.xy.titles.at(i) = "Diff'd "+final_results.xy.titles.at(i);
        }
        else if(modification_choice == 22){
          final_results.xy.titles.at(i) = "Summed "+final_results.xy.titles.at(i);
        }
      }
      break;
    }
    /////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////
    case 100:{
      cout<<"Loading the folder."<<endl;
      load_output(final_results,plot_options,grid);
      relative_reduction(final_results);
      break;
    }
  }//End o switch-case-break.
  
  //Set the output filename base so that these output files are final.
  base = "plot_data";
  
  //Now that the analysis has been performed, it's time to output the data
  //in a format that the IDL program grace_input_map.pro can turn into a 
  //plot.  Last variable is 1 because this is the final write_output.
  write_output(base, final_results, grid, plot_options,1);

  return 0;
}
